import com.sun.source.tree.IfTree;

class ListNode {
    int val;
    ListNode next = null;

    public ListNode(int val) {
        this.val = val;
    }
}

public class test_2 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param head1 ListNode类
     * @param head2 ListNode类
     * @return ListNode类
     */
    public static ListNode addInList(ListNode head1, ListNode head2) {
        ListNode node1 = my_reverse(head1);
        ListNode node2 = my_reverse(head2);
        int tmp = 0;
        ListNode ret = null;
        ListNode head = null;

        while (node1 != null || node2 != null) {
            int num_sl = 0;
            int nun_tl = 0;
            if (node1 != null) {
                num_sl = node1.val;
                node1 = node1.next;
            }
            if (node2 != null) {
                nun_tl = node2.val;
                node2 = node2.next;
            }
            tmp = num_sl + nun_tl + tmp;
            ListNode tmp_node = new ListNode(tmp % 10);
            if (ret == null) {
                ret = tmp_node;
                head = tmp_node;
            } else {
                ret.next = tmp_node;
                ret = ret.next;
            }

            tmp /= 10;

        }
        if (tmp != 0) {
            ret.next = new ListNode(tmp);
            ret = ret.next;
        }
        return my_reverse(head);
    }

    private static ListNode my_reverse(ListNode head) {
        if (head.next == null) {
            return head;
        }
        ListNode new_head = my_reverse(head.next);
        head.next.next = head;
        head.next = null;
        return new_head;
    }

    public static void main(String[] args) {
        ListNode head1 = new ListNode(1);
        ListNode head2 = new ListNode(2);
        ListNode head3 = new ListNode(3);
        head1.next = head2;
        head2.next = head3;
        ListNode node = my_reverse(head1);
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }
}
